package com.nbjtjc.safe.service.danger;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.DangerSafePlanListMapper;
import com.nbjtjc.safe.mapper.danger.DangerSafePlanMapper;
import com.nbjtjc.safe.model.danger.DangerSafePlan;
import com.nbjtjc.safe.model.danger.DangerSafePlanList;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/4/10 9:02
 **/
@Transactional(rollbackFor = {Throwable.class})
@Service
public class DangerSafePlanService {

    @Autowired
    private DangerSafePlanMapper dangerSafePlanMapper;

    @Autowired
    private DangerSafePlanListMapper dangerSafePlanListMapper;
    public Result add(DangerSafePlan dangerSafePlan){
        Result result=new Result();
        if(ObjectUtil.isNull(dangerSafePlan)){
            result.setFalse("不能为空");
            return  result;
        }
        dangerSafePlan.setCreateDate(new Date());
        dangerSafePlan.setCreateUserId(MyShiroUtils.getSessionUserId());
        dangerSafePlan.setCompanyId(MyShiroUtils.getCompanyId());
        dangerSafePlan.setDeptId(MyShiroUtils.getDeptId());
        dangerSafePlanMapper.insertSelective(dangerSafePlan);
        result.setData(dangerSafePlan);
        return result;
    }
    public Result delete(DangerSafePlan dangerSafePlan){
        Result result=new Result();
        dangerSafePlanMapper.deleteByPrimaryKey(dangerSafePlan);
        dangerSafePlanListMapper.deletePlanListByPlanId(dangerSafePlan.getId());
        return result;
    }
    public Result update(DangerSafePlan dangerSafePlan){
        Result result=new Result();
        dangerSafePlanMapper.updateByPrimaryKeySelective(dangerSafePlan);
        result.setData(dangerSafePlan);
        return result;
    }

    public Result find(DangerSafePlan dangerSafePlan){
        Result result=new Result();
        PageHelper.startPage(dangerSafePlan.getPageCurrent(),dangerSafePlan.getPageSize());
        List<DangerSafePlan> dangerSafePlans=dangerSafePlanMapper.search(dangerSafePlan);
        if(CollectionUtil.isEmpty(dangerSafePlans)){
            result.setData(new PageInfo<>(new ArrayList<DangerSafePlan>()));
        }else{
            result.setData(new PageInfo<>(dangerSafePlans));
        }
        return result;
    }

    public DangerSafePlan getDetail(DangerSafePlan dangerSafePlan){
        dangerSafePlan.setCompanyId(MyShiroUtils.getCompanyId());
        List<DangerSafePlan> dangerSafePlans= dangerSafePlanMapper.search(dangerSafePlan);
        if(CollectionUtil.isEmpty(dangerSafePlans)){
            return dangerSafePlan;
        }
        dangerSafePlan=dangerSafePlans.get(0);
        DangerSafePlanList dangerSafePlanList=new DangerSafePlanList();
        dangerSafePlanList.setSafePlanId(dangerSafePlan.getId());
        List<DangerSafePlanList> dangerSafePlanLists=dangerSafePlanListMapper.search(dangerSafePlanList);
        if(CollectionUtil.isEmpty(dangerSafePlanLists)){
            return dangerSafePlan;
        }
        dangerSafePlan.setDangerSafePlanLists(dangerSafePlanLists);
        return dangerSafePlan;
    }
}
